package com.bloveoacom.exception;

import android.util.MalformedJsonException;

import com.blankj.utilcode.util.NetworkUtils;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;


import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.io.IOException;
import java.io.NotSerializableException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLPeerUnverifiedException;

import retrofit2.HttpException;


/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/4/18
 * desc  : 异常
 */
public class ApiException extends IOException {

    private final int code;
    private String message;

    public ApiException(Throwable throwable, int code) {
        super(throwable);
        this.code = code;
        this.message = throwable.getMessage();
    }

    public ApiException(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (!NetworkUtils.isConnected()) {
            ex = new ApiException(e, ERROR.UNKNOWNHOST_ERROR);
            ex.message = "网络似乎出了点问题";
            return ex;
        } else if (e instanceof ApiException) {
            return (ApiException) e;
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ApiException(httpException, httpException.code());
            ex.message = "请求失败,请稍后重试";
            return ex;
        } else if (e instanceof SocketTimeoutException) {
            ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
            ex.message = "连接超时";
            return ex;
        } else if (e instanceof ConnectException) {
            ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
            ex.message = "网络似乎出了点问题";
            return ex;
        } else if (e instanceof ConnectTimeoutException) {
            ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
            ex.message = "网络似乎出了点问题";
            return ex;
        } else if (e instanceof UnknownHostException) {
            ex = new ApiException(e, ERROR.UNKNOWNHOST_ERROR);
            ex.message = "无法解析该域名";
            return ex;
        } else if (e instanceof NullPointerException) {
            ex = new ApiException(e, ERROR.NULL_POINTER_EXCEPTION);
            ex.message = "空指针异常";
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException
                || e instanceof SSLPeerUnverifiedException
                || e instanceof SSLException) {
            ex = new ApiException(e, ERROR.SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else if (e instanceof ClassCastException) {
            ex = new ApiException(e, ERROR.CAST_ERROR);
            ex.message = "类型转换错误";
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof JsonSerializer
                || e instanceof NotSerializableException
                || e instanceof ParseException
                || e instanceof MalformedJsonException
                || e instanceof com.google.gson.stream.MalformedJsonException) {
            ex = new ApiException(e, ERROR.PARSE_ERROR);
            ex.message = "服务器数据解析错误";
            return ex;
        } else if (e instanceof IllegalStateException) {
            ex = new ApiException(e, ERROR.ILLEGAL_STATE_ERROR);
            ex.message = e.getMessage();
            return ex;
        } else {
            ex = new ApiException(e, ERROR.UNKNOWN);
            ex.message = "未知错误";
            return ex;
        }
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    /**
     * 约定异常
     */
    public static class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 连接超时
         */
        public static final int TIMEOUT_ERROR = 1001;
        /**
         * 空指针错误
         */
        public static final int NULL_POINTER_EXCEPTION = 1002;

        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1003;

        /**
         * 类转换错误
         */
        public static final int CAST_ERROR = 1004;

        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1005;

        /**
         * 非法数据异常
         */
        public static final int ILLEGAL_STATE_ERROR = 1006;

        /**
         * 未知主机错误
         */
        public static final int UNKNOWNHOST_ERROR = 1007;


        //服务器约定的异常-------------------------------------
        /**
         * 帐号在其它手机已登录
         */
        public static final int OTHER_PHONE_LOGIN = -200;

        /**
         * AccessToken错误或已过期
         */
        public static final int ACCESS_TOKEN_EXPIRED = 10008;
        /**
         * RefreshToken错误或已过期
         */
        public static final int REFRESH_TOKEN_EXPIRED = 10009;

        /**
         * 时间戳过期
         */
        public static final int TIMESTAMP_ERROR = 10010;
        /**
         * 缺少授权信息,没有AccessToken
         */
        public static final int NO_ACCESS_TOKEN = 10011;
        /**
         * 签名错误
         */
        public static final int SIGN_ERROR = 10012;
    }
}
